﻿using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Terran
{
	public abstract class ActionUpgradeFromTerran : ActionProduceTerran
	{
		public abstract TerranState.TerranUpgrade GetUpgrade(TerranState state);
		public abstract TerranState.TerranProductionBuilding GetProductionBuilding(TerranState state);
		public abstract string EventName { get; }

		protected ActionUpgradeFromTerran(int researchTime, int minerals, int gas) : base(researchTime, minerals, gas, 0) { }

		public override int AvailableProducer(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return GetProductionBuilding(state).AvailableForUpgrade;
		}

		public override void Execute(IBuildOrder bo)
		{
			base.Execute(bo);
			TerranState state = bo.State as TerranState;
			GetProductionBuilding(state).Upgrade++;
			GetUpgrade(state).Researching = true;
			bo.AddAction(state.Time + BuildTime, new ActionGeneric(EventName, delegate(IBuildOrder ibo)
			{
				TerranState istate = ibo.State as TerranState;
				GetProductionBuilding(istate).Upgrade--;
				GetUpgrade(state).Researched();
			}));
		}

		public override bool IsInvalid(Jay.Sc2.Bof.Lib.State.IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return base.IsInvalid(bo) || GetUpgrade(state).Have || GetProductionBuilding(state).Total == 0;
		}

		public override bool HaveTechnology(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return GetProductionBuilding(state).Count > 0;
		}

		protected IAction[,] m_Requirements;
	}
}
